package com.onpositive.units;

import java.util.ArrayList;
import java.util.Collection;
import java.util.Collections;

import org.w3c.dom.Document;
import org.w3c.dom.Element;

import com.onpositive.knowledge.model.values.IHasValue;

public class ParsedLinkArray extends ParsedValue {
	
	/**
	 * 
	 */
	private static final long serialVersionUID = -5559459919394275033L;
	@Override
	public int hashCode() {
		final int prime = 31;
		int result = 1;
		result = prime * result + ((value == null) ? 0 : value.hashCode());
		return result;
	}

	@Override
	public boolean equals(Object obj) {
		if (this == obj)
			return true;
		if (obj == null)
			return false;
		if (getClass() != obj.getClass())
			return false;
		ParsedLinkArray other = (ParsedLinkArray) obj;
		if (value == null) {
			if (other.value != null)
				return false;
		} else if (!value.equals(other.value))
			return false;
		return true;
	}

	public static final String ID = "link-array" ;
	
	ArrayList<String> value ;
	public ParsedLinkArray( ArrayList<String> value, String comment ) {
		super(comment) ;
		this.value=value;
	}
	
	public ParsedLinkArray( ParsedValue pv, String comment )
	{
		super( comment ) ;		
		ParsedLinkArray other = (ParsedLinkArray) pv ;
		this.value = new ArrayList<String>( other.value );		
	}
	
	private ParsedLinkArray( ParsedLinkArray other, ArrayList<String> value,String comment )
	{
		super( comment ) ;	
		this.value = value ;		
	}
	
	public ParsedLinkArray( Element element ) {
		
		super(element) ;
		int i = 0 ;
		String link = element.getAttribute( "link"+i ) ;
		
		if( link != null && link != "" )
			value = new ArrayList<String>();
		
		for( ; link != null && link != "" ;  )
		{			
			value.add( link ) ;
			link = element.getAttribute( "link"+ ++i ).trim() ;
		}
	}

	@Override
	public Object value() {
		return value;
	}

	@Override
	public Object describer() {
		return String.class;
	}

	@Override
	public void writeValue(Element element){
		
		super.writeValuePartial(element) ;
		
		if( value == null )
			return ;
		
		int s = value.size() ;		
		for( int i = 0 ; i < s ; i++ )
			element.setAttribute( "link"+i, value.get(i) ) ;				
	}

	@Override
	public String getId() {
		return ID;
	}

	public ArrayList<String> getLinksArray() {
		return value;
	}

	@Override
	public String getValueAsString() {
		
		String result = "" ;
		if( value != null && value.size() > 0 )
		{
			result += embrace( value.get(0) ) ;
			for( int i = 1 ; i < value.size() ; i++ )
				result += ("; "+embrace( value.get(i) )) ;			
		}
		return result;
	}
	
	private String embrace( String s )
	{
		return "[[" + s + "]]" ;
	}

	@Override
	public void setValue(Object obj) {
		// TODO Auto-generated method stub
		
	}

	@Override
	public IHasValue clone(Object value, String comment ) {
		if( !(value instanceof ArrayList<?> ))
			return null ;
		if( comment == null )
			comment = this.getComment() ;
		return new ParsedLinkArray( this, (ArrayList<String>)value, comment ) ;
	}

	@Override
	public <T> Collection<T> getValues(Class<T> vs) {
		return Collections.emptySet();
	}

}
